package org.iabako.client.ui.form.business.sale;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.business.SearchForm;
import org.iabako.client.ui.form.business.util.SearchActionHandler;
import org.iabako.client.ui.layout.field.InputTextDate;
import org.iabako.client.ui.layout.field.SelectList;
import org.iabako.client.ui.layout.widget.customField.CustomFieldsInstance;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.shared.entity.business.Service;
import org.iabako.shared.iservice.business.PastInstallmentServiceAsync;
import org.iabako.shared.iservice.business.ServiceServiceAsync;
import org.iabako.shared.querydefinition.columndefinition.PastInstallmentColumnEnum;
import org.iabako.shared.querydefinition.dto.QueryOperatorEnum;
import org.iabako.shared.tools.GenericTools;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jose on 05/09/14.
 */
public class PastInstallmentSearchForm extends Composite {

    @UiTemplate("org.iabako.client.ui.form.business.SearchForm.ui.xml")
    interface SearchFormTemplate extends UiBinder<HTMLPanel, SearchForm> {
    }

    interface PastInstallmentSearchTemplate extends UiBinder<HTMLPanel, PastInstallmentSearchForm> {
    }

    private static final SearchFormTemplate binderSearchForm = GWT.create(SearchFormTemplate.class);
    private static final PastInstallmentSearchTemplate binderPastInstallmentSearchForm = GWT.create(PastInstallmentSearchTemplate.class);

    private final PastInstallmentServiceAsync pastInstallmentServiceAsync = PastInstallmentServiceAsync.Util.getInstance();
    @UiField
    InputTextDate installmentAfter;
    @UiField
    InputTextDate installmentBefore;
    @UiField
    SelectList service;
    @UiField
    CustomFieldsInstance customFieldsInstance;

    SearchForm searchForm;

    public PastInstallmentSearchForm(DefaultSuggestHandler serviceSuggestHandler, DataTable dataTable) {
        searchForm = new SearchForm() {
            @Override
            protected void addSpecificCriteria() {
                addPastInstallmentCriteria();
            }

            @Override
            protected void cleanSpecificCriteria() {
                cleanPastInstallmentCriteria();
            }
        };

        HTMLPanel searchFormPanel = binderSearchForm.createAndBindUi(searchForm);
        HTMLPanel pastInstallmentSearchPanel = binderPastInstallmentSearchForm.createAndBindUi(this);

        searchForm.addToDisclosureFormPanelContent(pastInstallmentSearchPanel);

        dataTable.deleteActionEnabled = false;

        searchForm.setProperties(pastInstallmentServiceAsync, serviceSuggestHandler, dataTable, "search_motor_past_installment_principal_input_title");
        searchForm.showClientCustomFieldsCriteria(customFieldsInstance);

        initialize();

        initWidget(searchFormPanel);

        setHandlers();
    }

    private void cleanPastInstallmentCriteria() {
        service.setRawValue(null);
        installmentAfter.setValue(null);
        installmentBefore.setValue(null);
    }

    private void addPastInstallmentCriteria() {
        if (!GenericTools.isEmpty(service.getValue())) {
            searchForm.addCriteria(PastInstallmentColumnEnum.serviceId, QueryOperatorEnum.IN, (Long) service.getValue());
        }
        if (!GenericTools.isEmpty(installmentAfter.getValue())) {
            searchForm.addCriteria(PastInstallmentColumnEnum.date, QueryOperatorEnum.GREATER_THAN_OR_EQUALS, installmentAfter.getDateValue());
        }
        if (!GenericTools.isEmpty(installmentBefore.getValue())) {
            searchForm.addCriteria(PastInstallmentColumnEnum.date, QueryOperatorEnum.LESS_THAN_OR_EQUALS, installmentBefore.getDateValue());
        }
    }

    private void initialize() {
        if (GenericTools.isEmpty(service.getValues())) {
            ServiceServiceAsync.Util.getInstance().getServices(new IabakoAsyncCallback<List<Service>>() {
                public void onSuccess(List<Service> result) {
                    Map<Long, String> services = new LinkedHashMap<Long, String>();
                    for (Service service : result) {
                        services.put(service.getId(), service.getName());
                    }
                    service.setValues(services);
                    super.onSuccess(result);
                }
            });
        }
    }

    private void setHandlers() {
        SearchActionHandler searchActionHandler = new SearchActionHandler(searchForm);

        service.getSelectField().addChangeHandler(searchActionHandler);
        installmentAfter.getInputFieldDate().addValueChangeHandler(searchActionHandler);
        installmentBefore.getInputFieldDate().addValueChangeHandler(searchActionHandler);
    }
}
